In [1]:
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import os.path as ospath
In [2]:
excelfile = ospath.join('..','Serious games','Games','БД4_Лёгкая.xlsm')
In [3]:
Alldata = pd.read_excel(excelfile,sheet_name='Данные')
Games = pd.read_excel(excelfile,sheet_name='Игры')
print(Alldata.columns)
Games.columns
Out[3]:
In [4]:
YHGames=Games[(Games.Mechname=='yh') & (Games.GameMech=='agree')]
YHExpGames=Games[(Games.Mechname=='yh') & (Games.GameMech=='exp')]
In [6]:
YHData=Alldata[Alldata.Game.isin(YHGames.Game)]
YHEData=Alldata[Alldata.Game.isin(YHExpGames.Game)]
In [7]:
print(YHData.shape,YHEData.shape)
In [7]:
GLGames=Games[(Games.Mechname=='gl') & (Games.GameMech=='agree')]
GLData=Alldata[Alldata.Game.isin(GLGames.Game)]
In [8]:
GLEGames=Games[(Games.Mechname=='gl') & (Games.GameMech=='exp')]
GLEData=Alldata[Alldata.Game.isin(GLEGames.Game)]
In [8]:
GLData.shape
Out[8]:
In [9]:
from scipy.spatial import distance
In [11]:
distance.cityblock([49,41,25],[38.3,38.3,38.3])
Out[11]:
In [ ]:
In [ ]:
In [64]:
gamelength=YHData[['Game','Time']].groupby('Game').max()
In [65]:
a=gamelength.sort_values(by='Time')
In [66]:
a['Game']=range(1,15)
In [67]:
b=a.rename(columns={'Time':'Game length'})
In [68]:
p=b.plot.scatter(x='Game',y='Game length')
In [69]:
p.get_figure().savefig('Gameslen.png',dpi=100)
In [31]:
GLData.axes
Out[31]:
In [40]:
GLData[['s1','s2','s3']].values[-1]
Out[40]:
In [23]:
for i,v in zip(GLData['Time'].values,GLData[['s1','s2','s3']].values):
print(type(v))
break
In [44]:
GLData['s2'].index
Out[44]:
In [8]:
def aheadCB(diter,eps,metric='cityblock'):
""" return next CB start and stop and remaining iterator """
from scipy.spatial import distance
from itertools import chain as mchain
start,vals = next(diter)
vals = [ vals ]
prev = start
for i,v in diter:
if np.asscalar(max(distance.cdist(vals,[v],metric))) > eps :
return ((start,prev),mchain([(i,v)],diter))
else:
prev = i
vals.append( v )
return ((start,prev),diter)
def getCB(data,eps=0,metric='cityblock'): # colon data[0] must be 1,2,3,... to properly work of data[end-1]
from itertools import chain as mchain
dend = data.index[-1]
(start,end),diter = aheadCB(zip(data.index.values,data.values),eps,metric=metric)
if start != end :
cblist = [(start,end)]
diter = mchain([(end,data.loc[end].values)],diter)
else :
cblist = []
while end != dend :
((start,end),diter) = aheadCB(diter,eps)
if start != end :
cblist.append((start,end))
diter = mchain([(end,data.loc[end].values)],diter)
return cblist
In [23]:
['Game','Time','GrSubject']+(['s2'] if 1 == 2 else ['s1','s2','s3'])
Out[23]:
In [9]:
def OnlySelfBidsOfGame(game): # s2 = sii
shiftgame = game.copy()
n = game.columns.get_loc('s1') - 1
shiftgame.loc[:,'s2']=shiftgame.apply(lambda x: x[n+x['GrSubject']], axis=1)
return shiftgame
def getCBwithEps(gamesdata, eps=0,dim=1,metric='cityblock'):
cols = ['s2'] if dim == 1 else ['s1','s2','s3']
gr = gamesdata.loc[:,['Game','Time','GrSubject']+cols].groupby(['GrSubject','Game'])
lst = []
for (s,g),data in gr:
data = data.set_index('Time')
glen = data.index.max()
if dim == 1:
cb = getCB(data[cols],eps,metric=metric)
else:
cb = getCB(data[cols],eps,metric=metric)
data = pd.DataFrame(cb,columns=['ts','te'])
data['subject'] = s
data['game'] = g
data['gamelength'] = glen
lst.append(data)
return pd.concat(lst).reset_index(drop=True)
def getCBofAllGames(gamesdata, maxeps=10, dim=1,metric='cityblock'):
if dim == 1:
gr = gamesdata.loc[:,['Game','Time','GrSubject','s2']].groupby(['GrSubject','Game'])
else:
gr = gamesdata.loc[:,['Game','Time','GrSubject','s1','s2','s3']].groupby(['GrSubject','Game'])
cbCounts, stepsCounts = [],[]
for eps in range(maxeps+1):
lst = []
for (s,g),data in gr:
data = data.set_index('Time')
if dim == 1:
cb = getCB(data[['s2']],eps,metric=metric)
else:
cb = getCB(data[['s1','s2','s3']],eps,metric=metric)
data = pd.DataFrame(cb,columns=['ts','te'])
data['subject'] = s
data['game'] = g
lst.append(data)
cb = pd.concat(lst).reset_index(drop=True)
cbCounts.append(cb.shape[0])
stepsCounts.append((cb['te']-cb['ts']+1).sum())
Counts = pd.DataFrame({'CB Count':cbCounts,'Steps Count':stepsCounts})
Counts.index.name = 'eps'
return Counts
def getCountsofCB(cbdata):
pass
In [70]:
gr = YHData.loc[:,['Game','Time','GrSubject','s2']].groupby('Game')
In [72]:
game40_1=gr.get_group(40)
#game40_1 = game40_1[['Time','s2']].set_index('Time')
In [76]:
game40_1.sort_values(by=['Time','GrSubject'])['s2'].iloc[-3:]
Out[76]:
In [ ]:
In [63]:
for i,v in game40_1.items():
print(i,v)
In [44]:
list(getCB(game40_1['s2'],1))
Out[44]:
In [9]:
GLCounts = getCBofAllGames(GLData,dim=3)
GLCounts['Steps Count %'] = GLCounts['Steps Count']/(2847/100)
GLCounts
Out[9]:
In [10]:
GLCounts.to_clipboard()
In [52]:
GLECounts = getCBofAllGames(GLEData,dim=3)
GLECounts['Steps Count %'] = GLECounts['Steps Count']/(60/100)
GLECounts
Out[52]:
In [53]:
print(YHData.shape,YHEData.shape)
In [55]:
Counts = getCBofAllGames(YHData)
Counts['Steps Count %'] = Counts['Steps Count']/(YHData.shape[0]/100)
Counts
Out[55]:
In [58]:
Counts.to_clipboard()
In [17]:
fig, axes = plt.subplots(nrows=2, ncols=1)
axes[0].set_title('CB Count')
axes[1].set_title('Steps Count')
Counts['CB Count'].plot(ax=axes[0])
Counts['Steps Count'].plot(ax=axes[1])
fig.tight_layout(h_pad=0.1)
fig.savefig('CB_StepsCount.png',dpi=100)
In [19]:
YHEData.shape
Out[19]:
In [56]:
CountsExp = getCBofAllGames(YHEData)
CountsExp['Steps Count %'] = CountsExp['Steps Count']/(YHEData.shape[0]/100)
CountsExp
Out[56]:
In [16]:
Meta={'basecount':YHData.shape[0],'expcount':YHEData.shape[0]}
In [27]:
fig, axes = plt.subplots(nrows=2, ncols=2)
axes[0,0].set_title('YH - CB Count')
axes[1,0].set_title('Experim. YH - CB Count')
axes[0,1].set_title('YH - Steps Count (%)')
axes[1,1].set_title('Experim. YH - Steps Count (%)')
CountsExp['CB Count'].plot(ax=axes[1,0])
(CountsExp['Steps Count']/(Meta['expcount']/100)).plot(ax=axes[1,1])
Counts['CB Count'].plot(ax=axes[0,0])
(Counts['Steps Count']/(Meta['basecount']/100)).plot(ax=axes[0,1])
fig.tight_layout(h_pad=0.1)
fig.set_size_inches((10,5))
fig.savefig('YHE_CB_StepsCount.png',dpi=150)
In [15]:
fig, axes = plt.subplots(nrows=2, ncols=1)
#axes[0,0].set_title('YH - CB Count')
#axes[1,0].set_title('Experim. YH - CB Count')
axes[0].set_title('YH - Steps Count (%)')
axes[1].set_title('Experim. YH - Steps Count (%)')
#CountsExp['CB Count'].plot(ax=axes[1,0])
CountsExp['Steps Count'].plot(ax=axes[1])
#Counts['CB Count'].plot(ax=axes[0,0])
Counts['Steps Count'].plot(ax=axes[0])
fig.tight_layout(h_pad=0.1)
#fig.set_size_inches((10,5))
fig.savefig('YHE_CB_StepsCount.png',dpi=150)
In [82]:
CountsExp['Steps Count']/75
Out[82]:
In [61]:
fig, axes = plt.subplots(nrows=2, ncols=1)
CountsExp['CB Count'].plot(ax=axes[0])
CountsExp['Steps Count'].plot(ax=axes[1])
fig.savefig('YHE_CB_StepsCount.png',dpi=100)
In [64]:
GLData[['Time','x']].iloc[-3:]
Out[64]:
NashOpt - оптимальное распределение ресурсов для максимизации функции торгов Нэша $ \Pi_i (u_i(x_i)-u_i(0)) $ :
In [ ]:
NashOpt=[32.0724,39.1355,43.7921]
In [115]:
gamegroups = GLData.groupby('Game')
a = []
for g,data in gamegroups:
data.sort_values(by=['Time','GrSubject'])
a.append( data['x'].iloc[-3:].values )
In [116]:
a
Out[116]:
In [81]:
from scipy.spatial import distance
In [119]:
npd = distance.cdist(a,[[115/3,115/3,115/3],[49,41,25],[32.0724, 39.1355, 43.7921]],'cityblock')
npd2 = distance.cdist(a,[[115/3,115/3,115/3],[49,41,25],[32.0724, 39.1355, 43.7921]],'euclidean')
[np.mean(npd,axis=0),np.mean(npd2,axis=0)]
Out[119]:
In [96]:
gamegroups = YHData.groupby('Game')
In [97]:
a = []
for g,data in gamegroups:
data.sort_values(by=['Time','GrSubject'])
a.append( data['x'].iloc[-3:].values )
In [99]:
npd = distance.cdist(a,[[49,41,25],[22.81,115/3,53.85]],'cityblock')
npd2 = distance.cdist(a,[[49,41,25],[22.81,115/3,53.85]],'euclidean')
In [100]:
np.mean(npd,axis=0)
Out[100]:
In [101]:
np.mean(npd2,axis=0)
Out[101]:
In [98]:
a
Out[98]:
In [ ]:
In [102]:
def f(x,r):
import math
return math.sqrt(r+x)-math.sqrt(r)
In [103]:
def Nf(x,r):
return f(x[0],r[0])*f(x[1],r[1])*f(x[2],r[2])
In [107]:
Nf([22.81,115/3,53.85],[1,9,25])
Out[107]:
In [105]:
Nf([20,40,55],[1,9,25])
Out[105]:
In [108]:
Nf([115/3,115/3,115/3],[1,9,25])
Out[108]:
In [109]:
22.81+115/3+53.85
Out[109]:
In [110]:
Nf([32.0724, 39.1355, 43.7921],[1,9,25])
Out[110]:
In [114]:
Nf([49,41,25],[1,9,25])
Out[114]:
In [10]:
cbs = getCBwithEps(YHData)
In [11]:
notStoppedCbs = cbs[cbs['te']!=cbs['gamelength']]
In [12]:
lengths = (notStoppedCbs['te']-notStoppedCbs['ts'])
In [13]:
lengths.values
Out[13]:
In [2]:
from scipy import stats
from scipy.stats import logser
from statsmodels.base.model import GenericLikelihoodModel
In [15]:
class LogFit(GenericLikelihoodModel):
def __init__(self, endog, exog=None, **kwds):
if exog is None:
exog = np.zeros_like(endog)
super(LogFit, self).__init__(endog, exog, **kwds)
def nloglikeobs(self, params):
return -np.log(logser.pmf(self.endog, p=params[0]))
def fit(self, start_params=None, maxiter=10000, maxfun=5000, **kwds):
if start_params is None:
excess_zeros = 0.5 #(self.endog == 0).mean() - stats.poisson.pmf(0, lambda_start)
start_params = np.array([excess_zeros])
return super(LogFit, self).fit(start_params=start_params,
maxiter=maxiter, maxfun=maxfun, **kwds)
In [20]:
results = LogFit(lengths.values.tolist()).fit()
In [21]:
plog = results.params.item()
In [22]:
plog
Out[22]:
In [66]:
notStoppedCbs.shape
Out[66]:
In [74]:
118*(1-logser.pmf(1,plog)-logser.pmf(2,plog)-logser.pmf(3,plog)-logser.pmf(4,plog)-logser.pmf(5,plog)-logser.pmf(6,plog)) #smallclass
Out[74]:
smallclass <~ 5 then use classes 1,2,3,4,5,[6,...]
In [108]:
def myclass(a):
if a > 4:
return 5
else:
return a
In [96]:
lengths.index = lengths.values
In [109]:
histclasses = lengths.groupby(myclass).count()
In [110]:
histclasses
Out[110]:
In [113]:
expclassesp = [logser.pmf(1,plog),logser.pmf(2,plog),logser.pmf(3,plog),logser.pmf(4,plog)]
expclassesp.append( 1 - np.sum(expclassesp) )
In [114]:
expclasses = np.multiply(118,expclassesp)
In [117]:
stats.chisquare(histclasses.values,expclasses,1)
Out[117]:
In [67]:
arr = np.loadtxt('2.txt')
In [61]:
np.shape(arr)
Out[61]:
In [46]:
stats.wilcoxon(arr[0],arr[1],zero_method='wilcox')
Out[46]:
In [23]:
d=pd.DataFrame(arr[0]-arr[1])
In [25]:
d['abs']=abs(d[0])
In [30]:
dsrt = d.sort_values(by='abs')
In [39]:
dsrtnn = dsrt[dsrt['abs']>0].copy()
In [40]:
dsrtnn['rank'] = range(1,dsrtnn.shape[0]+1)
In [45]:
dsrtnn[dsrtnn[0]<0]['rank'].sum()
Out[45]:
In [51]:
(np.sign(dsrtnn[0])*dsrtnn['rank']).sum()
Out[51]:
In [52]:
from math import sqrt
In [53]:
n = 125
sigma = sqrt( n*(n+1)*(2*n+1)/6 )
In [54]:
sigma
Out[54]:
In [55]:
sigma*1.96
Out[55]:
In [57]:
2*(1-stats.norm.cdf(3655.0/sigma))
Out[57]:
In [68]:
stats.mannwhitneyu(arr[0],arr[1],alternative='less')
Out[68]:
In [7]:
YHg=YHData.groupby(['Game','Time'])
In [46]:
YHData.shape
Out[46]:
In [32]:
YHg.sum()
Out[32]:
In [8]:
class Game:
import numpy as np
def __init__(self,R,s0,types):
self.R = R
self.s0 = np.array(s0)
self.n = len(s0)
self.types = np.array(types)
def u(self,x):
return self.np.sqrt(self.types+x)
game = Game( 115,(115/3,115/3,115/3), (1,9,25))
In [9]:
class Mechanism:
def __init__(self,game,params,xfunc,tfunc):
self.game = game
for k,v in params.items():
setattr(self,k,v)
self.xfunc = xfunc
self.tfunc = tfunc
def x(self,s):
return self.xfunc(s,self.game)
def t(self,s):
return self.tfunc(s,self.game,self)
def f(self,s):
return game.u(self.x(s))-self.t(s)
YHMechanism = Mechanism(game,
{'beta':0.0005},
lambda s,g: s*g.R/s.sum(),
lambda s,g,m: m.beta*s*(np.repeat(s.sum(),3)-s))
class GLClass:
from scipy.spatial import distance
def __init__(self):
self.glx = lambda s,g: s.sum(axis=0) / g.n
def glt(self,s,g,m):
p = m.beta * self.distance.cdist([self.glx(s,g)],s,'sqeuclidean')
pmean = m.alfa * p.mean()
return p - pmean
GL = GLClass()
GLMechanism = Mechanism(game,{'beta':0.0005,'alfa':1},
GL.glx,
GL.glt
)
In [104]:
s = np.array([[70,33.248853,11.751147],[26.5,77,11.5],[21.143719,33.856281,60]])
print(GLMechanism.x(s),GLMechanism.t(s),GLMechanism.f(s))
In [101]:
GLData.iloc[1800:1803]
Out[101]:
In [126]:
p=YHData.iloc[21:24]
p.sort_values(by="GrSubject")
print(p)
In [11]:
print(YHMechanism.beta)
s = np.array([1.2,40,80])
print(YHMechanism.t(s))
YHMechanism.f(s)
Out[11]:
In [12]:
def Unash(f,f0):
return (f-f0).prod()
print(YHMechanism.game.s0)
f0 = YHMechanism.game.u([0,0,0])
print(f0)
Unash(f0,s)
Out[12]:
In [115]:
import itertools
res = []
for name,group in YHg:
group = group.sort_values(by='GrSubject')
if name[1] == 1 :
curname = name[0]
prevg = group
continue
fprev = prevg['Gain'].values
sprev = prevg['s2'].values
s = group['s2'].values
s1, s2, s3 = sprev.copy(), sprev.copy(), sprev.copy()
s1[0] = s[0]
s2[1] = s[1]
s3[2] = s[2]
f1 = YHMechanism.f(s1)
f2 = YHMechanism.f(s2)
f3 = YHMechanism.f(s3)
U = Unash(fprev,f0)
U1 = Unash(f1,f0)
U2 = Unash(f2,f0)
U3 = Unash(f3,f0)
Unew = Unash(YHMechanism.f(s),f0)
res.append( [a for a in itertools.chain(name,[U1>U,U2>U,U3>U,Unew>U,U,U1,U2,U3,Unew])] )
prevg = group
#print(name,prev['Gain'])
In [116]:
a = np.vstack(res)
In [117]:
pda = pd.DataFrame(a); pda
Out[117]:
In [109]:
pda = pda[[2,3,4,5]]
Игры с механизмом YH и механизмом остановки "по договорённости" - никто не поменял заявок. Всего шагов 1035, если брать для каждого игрока в отдельности, то 3105 = 1035 * 3
Функция Нэша: $ U(f,f^0)=\prod_{i} (f_i -f_i^0) $
Базовые выигрыши: $ f^0 = u(0,0,0) $
Определения для текущего шага $t>1$: $$ U_{prev} = U(f(s^{t-1}),f^0) $$ $$ U_i =U(f(s_i^t,s_{-i}^{t-1}),f^0) $$ $$ U_{new}=U(f(s^t),f^0) $$
In [118]:
pda[pda[2]>0]
Out[118]:
In [110]:
a = (pda[pda>0].count()).values
In [113]:
pd.DataFrame({'Критерии':['U1 > Uprev','U2 > Uprev','U3 > Uprev','U1>Uprev & U2>Uprev & U3>Uprev',
'U1>Uprev & U2>Uprev & U3>Uprev & Unew > Uprev','Unew > Uprev',
'Unew > Uprev & Exist Ui < Uprev','Unew > Uprev & All Ui < Uprev'],
'Число подходящих ходов':[a[0],a[1],a[2],pda[(pda[2]*pda[3]*pda[4]==1)].shape[0],
pda[pda[2]*pda[3]*pda[4]*pda[5]==1].shape[0],
pda[pda[5]==1].shape[0],pda[(pda[5]==1) & (pda[2]*pda[3]*pda[4]==0)].shape[0],
pda[(pda[5]==1) & (pda[2]+pda[3]+pda[4]==0)].shape[0]
]
})
Out[113]:
In [13]:
YH40 = YHData[YHData['Game']==40].groupby(['Game','Time'])
In [15]:
YH40.sum()
Out[15]: